package com.ramos.kuaixiao.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.ramos.kuaixiao.App;
import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.adapter.MenuAdapter;
import com.ramos.kuaixiao.adapter.ParentAdapter;
import com.ramos.kuaixiao.db.BrandDB;
import com.ramos.kuaixiao.db.BrandInfoDB;
import com.ramos.kuaixiao.db.BrandKeywordsDB;
import com.ramos.kuaixiao.db.ShopsDB;
import com.ramos.kuaixiao.entity.BrandKeywords;
import com.ramos.kuaixiao.entity.Customer;
import com.ramos.kuaixiao.entity.ShopNew;
import com.ramos.kuaixiao.entity.ShopTypeBean;
import com.ramos.kuaixiao.utils.CommonLoading;
import com.ramos.kuaixiao.utils.NetUtils;
import com.ramos.kuaixiao.utils.SharedPreference;
import com.ramos.kuaixiao.utils.StringUtil;
import com.ramos.kuaixiao.utils.ToastUtils;

import org.apache.http.Header;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ShopOrderActivity extends BaseAllTabActivity implements OnClickListener {
    private static String TAG = "ShopOrderActivity";
    private Context mContext;
    public final static int SCANNIN_GREQUEST_CODE = 0xb1;

    private List<String> myTypeList = new ArrayList<>();
    private List<ShopTypeBean.ShopType> shopTypeList = new ArrayList<>();
    //private List<ShopTypeBean.ShopType.Type> typeList = new ArrayList<>();
    private ListView lv_brand;
    private ExpandableListView lv_brand_child;

    private MenuAdapter myMenuAdapter;
    private ParentAdapter myParentAdapter;

    private ImageView saoyisao;
    private LinearLayout rl_choose;
    private LinearLayout ll_unfinished_none;
    private List<ShopNew> shopList = new ArrayList<>();

    BrandInfoDB mBrand;
    public final static int UPLOAD_SUCCESS = 100;
    public final static int UPLOAD_FAIL = 101;
    public final static int DETAIL_SUCCESS = 200;
    public final static int DETAIL_FAIL = 201;
    Customer mCustomer;
    private CommonLoading loading;
    private CommonLoading loading1;
    TextView searchview;
    RelativeLayout bottom;
    private RelativeLayout re_home_shop;
    private RelativeLayout re_home_order;
    private RelativeLayout re_home_task;
    private RelativeLayout re_home_my;
    private TextView tv_home_shop;
    private TextView tv_home_order;
    private TextView tv_home_task;
    private TextView tv_home_my;
    private ImageView ib_home_shop;
    private ImageView ib_home_order;
    private ImageView ib_home_task;
    private ImageView ib_home_my;

    String mCurrentBrand;
    String mIntentBrand;
    int mPosition;
    BrandDB mBrandDB;
    int i = 0;
    List<BrandKeywords> mBrandKeywords = new ArrayList<>();
    public static List<ShopTypeBean.ShopType.ProductInfo> mProducts = new ArrayList<>();
    List<ShopTypeBean.ShopType.Type> mCurrentTypelist = new ArrayList<>();
    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_shop_new);
        initView();
        initLocalDB();
    }

    private void initLocal() {
        sendMessagea(UPLOAD_SUCCESS, App.SHOP_RESULT);
    }

    private void initView() {
        mContext = this;
        mBrand = new BrandInfoDB();
        mCustomer = (Customer) getIntent().getExtras().get("customer");
        mIntentBrand = (String) getIntent().getExtras().get("brand");
        mPosition = (Integer) getIntent().getExtras().get("position");
        rl_choose = (LinearLayout) findViewById(R.id.rl_choose);
        rl_choose.setOnClickListener(this);
        ll_unfinished_none = (LinearLayout) findViewById(R.id.ll_unfinished_none);
        bottom = (RelativeLayout) findViewById(R.id.bottom);
        saoyisao = (ImageView) findViewById(R.id.saoyisao);
        saoyisao.setBackgroundColor(Color.TRANSPARENT);
        saoyisao.setOnClickListener(this);

        //显示品牌列表
        showBrand();

        searchview = (TextView) findViewById(R.id.searchview);
        searchview.setOnClickListener(this);

        initTabView();
    }

    private void initTabView() {
        re_home_shop = (RelativeLayout) findViewById(R.id.re_home_shop);
        re_home_order = (RelativeLayout) findViewById(R.id.re_home_order);
        re_home_task = (RelativeLayout) findViewById(R.id.re_home_task);
        re_home_my = (RelativeLayout) findViewById(R.id.re_home_my);
        re_home_shop.setOnClickListener(this);
        re_home_order.setOnClickListener(this);
        re_home_task.setOnClickListener(this);
        re_home_my.setOnClickListener(this);

        ib_home_shop = ((ImageView) findViewById(R.id.ib_home_shop));
        ib_home_order = ((ImageView) findViewById(R.id.ib_home_order));
        ib_home_task = ((ImageView) findViewById(R.id.ib_home_task));
        ib_home_my = ((ImageView) findViewById(R.id.ib_home_my));

        tv_home_shop = ((TextView) findViewById(R.id.tv_home_shop));
        tv_home_order = ((TextView) findViewById(R.id.tv_home_order));
        tv_home_task = ((TextView) findViewById(R.id.tv_home_task));
        tv_home_my = ((TextView) findViewById(R.id.tv_home_my));

        ib_home_shop.setImageResource(R.drawable.tab_infomation_clk);
        tv_home_shop.setTextColor(getResources().getColor(R.color.title));

        ib_home_order.setImageResource(R.drawable.tab_schedule);
        tv_home_order.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        ib_home_task.setImageResource(R.drawable.tab_service);
        tv_home_task.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        ib_home_my.setImageResource(R.drawable.tab_contacts);
        tv_home_my.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

    }

    private void showBrand() {
        lv_brand = (ListView) findViewById(R.id.lv_brand);
        lv_brand_child = (ExpandableListView) findViewById(R.id.lv_brand_child);
        myMenuAdapter = new MenuAdapter(mContext, myTypeList);
        lv_brand.setAdapter(myMenuAdapter);

        myParentAdapter = new ParentAdapter(mContext, mCurrentTypelist);
        lv_brand_child.setAdapter(myParentAdapter);
        lv_brand_child.setGroupIndicator(null);
        //lv_brand_child.expandGroup(0);
        lv_brand.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                myMenuAdapter.setSelectItem(position);
                myMenuAdapter.notifyDataSetInvalidated();
                myParentAdapter.refresh(null);

                //String products = mBrandDB.getProducts(shopList.get(position).getBrandId());
                //loadDbData(products);

                mCurrentBrand = shopList.get(position).getBrandName();
                //Log.d(TAG, "products:"+products);
                List<BrandKeywords> list = new ArrayList<>();
                list.clear();
                for (BrandKeywords keywords : mBrandKeywords) {
                    if (keywords.getBrand_id().contains(shopList.get(position).getBrandId())) {
                        list.add(keywords);
                    }
                }
                Log.d(TAG, "keywords:" + list.size());
                initKeyWordsList(list);
            }
        });

        lv_brand_child.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                boolean groupExpanded = parent.isGroupExpanded(groupPosition);
                Log.d(TAG, "groupExpanded " + groupExpanded);
                return false; // 默认为false，设为true时，点击事件不会展开Group
            }
        });

        lv_brand_child.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                ShopTypeBean.ShopType.Type shopType = (ShopTypeBean.ShopType.Type) myParentAdapter.getGroup(groupPosition);
                ShopTypeBean.ShopType.Product type = myParentAdapter.getChild(groupPosition, childPosition);
                //List<ShopTypeBean.ShopType.ProductInfo> productList = type.getProductInfoNameList();
                List<ShopTypeBean.ShopType.ProductInfo> productList = getProductList(mCurrentBrand, shopType.getTypeName(), type.getTypeName(), mCurrentTypelist);
                Intent intent = new Intent(mContext, ProductResultActivity.class);
                intent.putExtra("result", (Serializable) productList);
                intent.putExtra("customer", mCustomer);
                intent.putExtra("brand", mCurrentBrand);
                intent.putExtra("position", childPosition);
                intent.putExtra("groupPosition", groupPosition);
                startActivity(intent);
                //searchType(productList);
                return false;
            }
        });
        lv_brand_child.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                for (int i = 0; i < mCurrentTypelist.size(); i++) {
                    if (groupPosition != i) {
                        lv_brand_child.collapseGroup(i);
                    }
                }
            }
        });
    }

    private List<ShopTypeBean.ShopType.ProductInfo> getProductList(String mCurrentBrand, String brandName, String typeName, List<ShopTypeBean.ShopType.Type> mCurrentTypelist) {
        List<ShopTypeBean.ShopType.ProductInfo> brandList = new ArrayList<>();
        brandList.clear();
        List<ShopTypeBean.ShopType.ProductInfo> list = new ArrayList<>();
        list.clear();
        Log.d(TAG, "getProductList:"+App.mProductInfos.size() + " " +mCurrentBrand + " " + brandName + " " + typeName + " " + mCurrentTypelist.size());
        if(App.mProductInfos.size() == 0){
            App.mProductInfos.clear();
            App.mProductInfos.addAll(new ShopsDB().findAllUseShops());
        }
        for(ShopTypeBean.ShopType.ProductInfo product : App.mProductInfos){
            if(product.getBrandName().equals(mCurrentBrand)){
                //Log.d(TAG, "product:"+product.getBrandName() + " " + product.getProductInfoName());
                brandList.add(product);
            }
        }
        Log.d(TAG, "brandList:" + brandList.size());
        if(brandName.equals("全部系列")){
            list.addAll(brandList);
        }
        else if(brandName.equals("其他")){
            if(mCurrentTypelist.size() == 2){
                if(typeName.equals("全部规格")){
                    list.addAll(brandList);
                }else {
                    for(ShopTypeBean.ShopType.ProductInfo product : brandList){
                        if(product.getProductInfoName().contains(typeName)){
                            list.add(product);
                        }
                    }
                }
            } else {
                List<ShopTypeBean.ShopType.ProductInfo> tmpList = new ArrayList<>();
                for(ShopTypeBean.ShopType.Type type : mCurrentTypelist){
                    if(!type.getTypeName().equals("其他") && !type.getTypeName().equals("全部系列")){
                        for(ShopTypeBean.ShopType.ProductInfo product : brandList){
                            if(product.getProductInfoName().contains(type.getTypeName())){
                                tmpList.add(product);
                            }
                        }
                    }
                }
                list.addAll(brandList);
                list.removeAll(tmpList);
            }
        } else {
            if(typeName.equals("全部规格")){
                for(ShopTypeBean.ShopType.ProductInfo product : brandList){
                    if(product.getProductInfoName().contains(brandName)){
                        list.add(product);
                    }
                }
            }else {
                for(ShopTypeBean.ShopType.ProductInfo product : brandList){
                    //Log.d(TAG, "product:" + product.getProductInfoName());
                    if(product.getProductInfoName().toLowerCase().contains(typeName.toLowerCase()) && product.getProductInfoName().contains(brandName)){
                        list.add(product);
                    }
                }
            }
        }
        Log.d(TAG, "getProductList:"+mCurrentBrand + " " + brandName + " " + typeName + " " + list.size() + " " + mCurrentTypelist.size());
        return list;
    }

    private String getBrand(String brandId, Map<String, String> mAllShopTypes) {
        String value = "";
        for (Map.Entry<String, String> entry : mAllShopTypes.entrySet()) {
            if (entry.getKey().equals(brandId)) {
                value = entry.getValue();
            }
        }
        return value;
    }

    private void initLocalDB() {
        mBrandDB = new BrandDB();
        //List<ShopNew> list = mBrandDB.findAllNoEmptyBrand();
        List<ShopNew> list = mBrandDB.findAllNoEmptyBrand();
        mBrandKeywords.addAll(new BrandKeywordsDB().findAll());

        initList(list);
/*        try {
            new Test().getProductByBrand();
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }

    private void initList(List<ShopNew> mshopList) {
        this.shopList = mshopList;
        for (int i = 0; i < shopList.size(); i++) {
            ShopNew shopType = shopList.get(i);
            myTypeList.add(shopType.getBrandName());
        }
        myMenuAdapter.notifyDataSetChanged();

        mCurrentBrand = mIntentBrand;
        myMenuAdapter.setSelectItem(mPosition);

        for (ShopNew shopNew : shopList) {
            if (shopNew.brandName.equals(mIntentBrand)) {
                //getParentData(shopNew.getBrandId());
                //暂时不使用解析分层
                //String products = mBrandDB.getProducts(shopNew.getBrandId());
                //loadDbData(products);
                //Log.d(TAG, "products:"+products);
                List<BrandKeywords> list = new ArrayList<>();
                list.clear();
                for (BrandKeywords keywords : mBrandKeywords) {
                    if (keywords.getBrand_id().contains(shopNew.getBrandId())) {
                        list.add(keywords);
                    }
                }
                Log.d(TAG, "keywords:" + list.size());
                initKeyWordsList(list);
            }
        }
    }

    private void initKeyWordsList(List<BrandKeywords> words) {
        mCurrentTypelist.clear();
        if (words == null || words.size() == 0) {
            myParentAdapter.refresh(null);
            lv_brand_child.setVisibility(View.GONE);
            ll_unfinished_none.setVisibility(View.VISIBLE);
        } else {
            try {
                List<ShopTypeBean.ShopType.Type> list = new ArrayList<>();
                for (BrandKeywords word : words) {
                    ShopTypeBean.ShopType.Type type = new ShopTypeBean.ShopType.Type();
                    type.setTypeName(word.getKeyword());
                    String subkeyword = word.getSub_keyword();
                    String[] sub_words = subkeyword.split("_");
                    List<ShopTypeBean.ShopType.Product> products1 = new ArrayList<>();
                    if (sub_words.length > 0) {
                        for (String s : sub_words) {
                            ShopTypeBean.ShopType.Product product = new ShopTypeBean.ShopType.Product();
                            product.setTypeName(s);
                            products1.add(product);
                        }
                    }
                    type.setTypeGroupList(products1);
                    list.add(type);
                }
                if (list.size() > 0) {
                    mCurrentTypelist.addAll(list);
                    lv_brand_child.setVisibility(View.VISIBLE);
                    ll_unfinished_none.setVisibility(View.GONE);
                    myParentAdapter = new ParentAdapter(mContext, list);
                    lv_brand_child.setAdapter(myParentAdapter);
                    for (int i = 0; i < list.size(); i++) {
                        lv_brand_child.expandGroup(0);
                    }
                } else {
                    myParentAdapter.refresh(null);
                    lv_brand_child.setVisibility(View.GONE);
                    ll_unfinished_none.setVisibility(View.VISIBLE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private void initShopNew(String s) {
        JSONObject jsonObejct = JSONObject.parseObject(s);
        JSONObject data = jsonObejct.getJSONObject("data");
        JSONArray array;
        if (data.getJSONArray("rows") == null) {
            if (data.getJSONArray("row") == null) {
                return;
            } else {
                array = data.getJSONArray("row");
            }
        } else {
            array = data.getJSONArray("rows");
        }
        shopList = JSONArray.parseArray(array.toString(), ShopNew.class);
        for (int i = 0; i < shopList.size(); i++) {
            ShopNew shopType = shopList.get(i);
            myTypeList.add(shopType.getBrandName());
        }
        myMenuAdapter.notifyDataSetChanged();

        mCurrentBrand = mIntentBrand;
        myMenuAdapter.setSelectItem(mPosition);

        for (ShopNew shopNew : shopList) {
            if (shopNew.brandName.equals(mIntentBrand)) {
                String products = mBrandDB.getProducts(shopNew.getBrandId());
                loadDbData(products);
                App.mAllShopTypes.put(shopNew.getBrandId(), products);
               /* if(StringUtil.isEmpty(products)) {
                    getParentData(shopNew.getBrandId());
                } else {
                    loadDbData(products);
                    App.mAllShopTypes.put(shopNew.getBrandId(), products);
                }*/
            }
        }
    }

    private void loadDbData(String products) {
        shopTypeList.clear();
        if (StringUtil.isEmpty(products)) {
            myParentAdapter.refresh(null);
            lv_brand_child.setVisibility(View.GONE);
            ll_unfinished_none.setVisibility(View.VISIBLE);
        } else {
            List<ShopTypeBean.ShopType.Type> list = JSONArray.parseArray(products, ShopTypeBean.ShopType.Type.class);
            if (list.size() == 1) {
                ShopTypeBean.ShopType.Type type = list.get(0);
                type.setTypeName("全部");
                ShopTypeBean.ShopType.Product product = new ShopTypeBean.ShopType.Product();
                product.setTypeName("全部");
                List<ShopTypeBean.ShopType.Product> products1 = type.getTypeGroupList();
                products1.add(0, product);
                type.setTypeGroupList(products1);
            } else {
                ShopTypeBean.ShopType.Type type = new ShopTypeBean.ShopType.Type();
                type.setTypeName("全部");
                ShopTypeBean.ShopType.Product product = new ShopTypeBean.ShopType.Product();
                product.setTypeName("全部");
                List<ShopTypeBean.ShopType.Product> products1 = new ArrayList<>();
                products1.add(product);
                type.setTypeGroupList(products1);
                list.add(0, type);
            }

            //JSONObject jsonObejct = JSONObject.parseObject(products);
            //JSONObject data = jsonObejct.getJSONObject("data");
            App.SHOP_RESULT = products;
            //ShopTypeBean shopTypeBean = JSONObject.parseObject(data.toString(), ShopTypeBean.class);
            try {
                //本地品牌和详情
               /* ShopTypeBean shopTypeBean = new Test().getProductByBrand();
                for (int i = 0; i < shopTypeBean.getBrandList().size(); i++) {
                    ShopTypeBean.ShopType shopType = shopTypeBean.getBrandList().get(i);
                    shopTypeList.add(shopType);
                    Log.d(TAG, "loadDbData:" + shopTypeList.size());
                }*/
               /*shopTypeList.addAll(list);
               if (shopTypeList != null && shopTypeList.size() > 0) {
                   lv_brand_child.setVisibility(View.VISIBLE);
                   ll_unfinished_none.setVisibility(View.GONE);
                   Log.d(TAG, "loadDbData:" + shopTypeList.get(0).getTypeGroupList().get(0).getTypeId() + " " + shopTypeList.get(0).getTypeGroupList().get(0).getTypeName()
                           + shopTypeList.get(0).getTypeGroupList().get(0).getTypeGroupList().size());
                   myParentAdapter = new ParentAdapter(mContext, shopTypeList.get(0).getTypeGroupList());
                   lv_brand_child.setAdapter(myParentAdapter);
                   if (shopTypeList.size() > 0) {
                       lv_brand_child.expandGroup(0);
                   }
               } else {
                   myParentAdapter.refresh(null);
                   lv_brand_child.setVisibility(View.GONE);
                   ll_unfinished_none.setVisibility(View.VISIBLE);
               }*/
                if (list.size() > 0) {
                    lv_brand_child.setVisibility(View.VISIBLE);
                    ll_unfinished_none.setVisibility(View.GONE);
                    myParentAdapter = new ParentAdapter(mContext, list);
                    lv_brand_child.setAdapter(myParentAdapter);
                    for (int i = 0; i < list.size(); i++) {
                        lv_brand_child.expandGroup(i);
                    }
                } else {
                    myParentAdapter.refresh(null);
                    lv_brand_child.setVisibility(View.GONE);
                    ll_unfinished_none.setVisibility(View.VISIBLE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private void getParentData(final String brandId) {
        try {
            String url = NetUtils.PRODUCT_INFO_DETAIL;
            String path = url + SharedPreference.getPreferString("dealer") + "&brandId=" + brandId
                    + "&userId=" + SharedPreference.getPreferString("id");
            AsyncHttpClient client = new AsyncHttpClient(); // 创建异步请求的客户端对象
            loading = new CommonLoading(this);
            loading.setLoadText("获取数据中...");
            // 创建请求参数的封装的对象
            client.setTimeout(10000);
            client.get(path, new AsyncHttpResponseHandler() {

                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    //statusCode:状态码    headers：头信息  responseBody：返回的内容，返回的实体
                    loading.dismiss();
                    //判断状态码
                    try {
                        String result = new String(responseBody, "utf-8");
                        Log.d(TAG, "getParentData " + result);
                        org.json.JSONObject user = new org.json.JSONObject(result);
                        if (user.getInt("status") == 200) {
                            sendMessagea(DETAIL_SUCCESS, result);
                            App.mAllShopTypes.put(brandId, result);
                        } else {
                            sendMessagea(DETAIL_FAIL, "fail");
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        sendMessagea(DETAIL_FAIL, "fail");
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers,
                                      byte[] responseBody, Throwable error) {
                    loading.dismiss();
                    sendMessagea(DETAIL_FAIL, "fail");
                }
            });
        } catch (Exception e) {
            loading.dismiss();
            e.printStackTrace();
        }
    }

    //获取品牌数据
    private void initShopOL() {
        try {
            String url = NetUtils.PRODUCT_INFO_ALL;
            String path = url + SharedPreference.getPreferString("dealer") + "&userId=" + SharedPreference.getPreferString("id");
            AsyncHttpClient client = new AsyncHttpClient(); // 创建异步请求的客户端对象
            loading1 = new CommonLoading(this);
            loading1.setLoadText("获取数据中...");
            // 创建请求参数的封装的对象
            client.setTimeout(10000);
            client.get(path, new AsyncHttpResponseHandler() {

                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    //statusCode:状态码    headers：头信息  responseBody：返回的内容，返回的实体
                    loading1.dismiss();
                    //判断状态码
                    try {
                        String result = new String(responseBody, "utf-8");
                        Log.d(TAG, "result " + result);
                        org.json.JSONObject user = new org.json.JSONObject(result);
                        if (user.getInt("status") == 200) {
                            App.SHOP_RESULT = result;
                            sendMessagea(UPLOAD_SUCCESS, result);
                        } else {
                            sendMessagea(UPLOAD_FAIL, "fail");
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        sendMessagea(UPLOAD_FAIL, "fail");
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers,
                                      byte[] responseBody, Throwable error) {
                    loading1.dismiss();
                    sendMessagea(UPLOAD_FAIL, "fail");
                }
            });
        } catch (Exception e) {
            loading1.dismiss();
            e.printStackTrace();
        }
    }

    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UPLOAD_SUCCESS:
                    Log.d(TAG, "result " + msg.obj.toString());
                    initShopNew(msg.obj.toString());
                    break;
                case UPLOAD_FAIL:
                    Log.d(TAG, "result fail");
                    ToastUtils.show("获取数据失败！");
                    break;
                case DETAIL_SUCCESS:
                    Log.d(TAG, "result " + msg.obj.toString());
                    loadData(msg.obj.toString());
                    break;
                case DETAIL_FAIL:
                    lv_brand_child.setVisibility(View.GONE);
                    ll_unfinished_none.setVisibility(View.VISIBLE);
                    ToastUtils.show("获取数据失败！");
                    break;
                default:
                    break;
            }
        }
    };

    private void loadData(String s) {
        shopTypeList.clear();
        JSONObject jsonObejct = JSONObject.parseObject(s);
        JSONObject data = jsonObejct.getJSONObject("data");
        ShopTypeBean shopTypeBean = JSONObject.parseObject(data.toString(), ShopTypeBean.class);
        for (int i = 0; i < shopTypeBean.getBrandList().size(); i++) {
            ShopTypeBean.ShopType shopType = shopTypeBean.getBrandList().get(i);
            shopTypeList.add(shopType);
        }
        if (shopTypeList != null && shopTypeList.size() > 0) {
            lv_brand_child.setVisibility(View.VISIBLE);
            ll_unfinished_none.setVisibility(View.GONE);
            myParentAdapter = new ParentAdapter(mContext, shopTypeList.get(0).getTypeGroupList());
            lv_brand_child.setAdapter(myParentAdapter);
            if (shopTypeList.size() > 0) {
                lv_brand_child.expandGroup(0);
            }
        } else {
            myParentAdapter.refresh(null);
            lv_brand_child.setVisibility(View.GONE);
            ll_unfinished_none.setVisibility(View.VISIBLE);
        }
    }

    public void sendMessagea(int iWhat, Object obj) {
        Message msg = Message.obtain();
        msg.what = iWhat;
        msg.obj = obj;
        mHandler.sendMessage(msg);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.saoyisao:
                Intent intent = new Intent();
                intent.setClass(mContext, CaptureActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
                break;
            case R.id.rl_choose:
                finish();
                break;
            case R.id.re_home_shop:
                showMain(1);
                break;
            case R.id.re_home_order:
                showMain(2);
                break;
            case R.id.re_home_task:
                showMain(3);
                break;
            case R.id.re_home_my:
                showMain(4);
                break;
            case R.id.searchview:
                Intent intent1 = new Intent(mContext, ProductResultActivity.class);
                intent1.putExtra("customer", mCustomer);
                startActivity(intent1);
            default:
                break;
        }
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (resultCode == SCANNIN_GREQUEST_CODE) {
                Log.d(TAG, "onActivityResult " + data.getExtras().getString("scan_code"));
                Intent intent = new Intent(ShopOrderActivity.this, ProductResultActivity.class);
                intent.putExtra("scan_code", data.getExtras().getString("scan_code"));
                startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
